< previous page page_115 next page >

Page 115
Notice that in Listing 5.2, you first tested to see whether the key pressed was greater than 26. ASCII code 26 is the last Visual Basic 5 control code. The routine in Listing 5.2 now skips over filtering of control codes. When you save and run the program, you can pass the plus, minus, and decimal point characters into the textbox, too.
Now let's create validation code that accepts only uppercase characters. This is a bit trickier. Instead of ignoring lowercase input, you convert it to upper case, and then pass it through to the textbox.
First add another label and textbox control. Set the label caption to Uppercase. Set the Name property of the textbox to txtUpper and set the text property to blank. Your form should look like the one in Figure 5.2.
0115-01.jpg
Figure 5.2.
Adding the Uppercase control
and conversion to the form.
The code needed for the txtUpper KeyPress event is in Listing 5.3. Even though there's only one line of code in this routine, there's a lot going on. This line of code (reading from the inside function outward) first coverts the Keyascii value into a printable character, converts that character to upper case, and then converts the character back to an ASCII numeric value. Notice that instead of setting the Visual Basic 5 KeyAscii parameter to zero (discarding it), this routine converts it to an uppercase value. This works no matter what key is pressed.
Type.gifListing 5.3. The KeyPress event to force letters to uppercase.
Private Sub txtUpper_KeyPress(KeyAscii As Integer)
    '
    KeyAscii = Asc(UCase(Chr(KeyAscii))) ' change to uppercase
    '
End Sub
When you save and run the program, you see that any letter key you enter converts to an uppercase letter and passes through to the textbox.

 
< previous page page_115 next page >

If you like this book, buy it!